package com.baselibrary.utils;

import android.content.Context;

import com.alibaba.android.arouter.utils.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author:hanke 2022/6/22
 * desc:
 */
public class AddressUtil {
    public static String getAddressName(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (!jsonObject.isNull("name")) {
                return jsonObject.getString("name");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getAddressId(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (!jsonObject.isNull("id")) {
                return jsonObject.getString("id");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getAddressDetail(Context context){
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(SpUtils.getString(context,"address_ProvinceName"))){
            sb.append(SpUtils.getString(context,"address_ProvinceName"));
        }
        if (!TextUtils.isEmpty(SpUtils.getString(context,"address_CityName")) && !SpUtils.getString(context,"address_ProvinceName").equals(SpUtils.getString(context,"address_CityName"))){
            sb.append(SpUtils.getString(context,"address_CityName"));
        }
        if (!TextUtils.isEmpty(SpUtils.getString(context,"address_AreaName"))){
            sb.append(SpUtils.getString(context,"address_AreaName"));
        }
        if (!TextUtils.isEmpty(SpUtils.getString(context,"address_Town"))){
            sb.append(SpUtils.getString(context,"address_Town"));
        }
        if (!TextUtils.isEmpty(SpUtils.getString(context,"address_Region"))){
            sb.append(SpUtils.getString(context,"address_Region"));
        }
        return sb.toString();
    }
}
